<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="140px">

      <el-form-item prop="companyName" :label="$t('companyName')">
        <el-select
                v-model="dataForm.companyId"
                filterable
                remote
                reserve-keyword
                placeholder="请输入公司名称关键词"
                :remote-method="remoteMethod"
                :loading="loading"
                style="width: 100%"  >
          <el-option
                  v-for="item in companyList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="money" :label="$t('money')">
        <el-input-number v-model="dataForm.money" :precision="2" :step="100" :max="50000"></el-input-number>
      </el-form-item>
      <el-form-item prop="operation" :label="$t('deal.operation')">
        <el-select
                v-model="dataForm.operation"
                placeholder="请选择交易类型"
                @change="changeOperation"
                :loading="loading"  >
          <el-option
                  v-for="item in operationTypes"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="paymentType" :label="$t('deal.paymentType')">
        <el-select
                v-model="dataForm.paymentType"
                placeholder="请选择支付方式"
                :loading="loading"  >
          <el-option
            v-for="dict in paymentTypes"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item prop="remark" :label="$t('remark')">
        <el-input type="textarea" v-model="dataForm.remark" :placeholder="$t('remark')"/>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { debounce } from 'lodash'
  export default {
    data () {
      return {
        visible: false,
        companyList: [],
        paymentTypes:[],
        operationTypes:[{name:'充值',value:1},{name:'消费',value: 2}],
        dataForm: {
          id: '',
          paymentType: "1",
          operation: 1,
          money: 500,
          companyId: '',
          remark: ''
        },
        loading: false,
      }
    },
    computed: {
      dataRule () {
        return {
          name: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          qualificationCode: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          Promise.all([
            this.getPaymentTypeList()
          ]).then(() => {
            if (this.dataForm.id) {
              this.getInfo()
            }
          })
        })
      },
      // 获取信息
      getInfo () {
        this.$axios.get(`/manager/lawyer/info/${this.dataForm.id}`).then(res => {
          this.dataForm = {
            ...this.dataForm,
            ...res
          }
        }).catch(() => {})
      },
      // 表单提交
      dataFormSubmitHandle: debounce(function () {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid) {
            return false
          }
          this.$http.json('/manager/companyAccount/addOrUpdate', this.dataForm).then(res => {
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => {})
        })
      }, 1000, { 'leading': true, 'trailing': false }),

      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.getCompanyList(query);
        } else {
          this.companyList = [];
        }
      },

      // 获企业客户所列表
      getCompanyList(queryName) {
        return this.$axios.get('/manager/company/page', {
          params: {
            order: 'ASC',
            orderField: 'createTime',
            current: 1,
            size: 20,
            listMode: true,
            queryItems: 'name,LIKE,' + queryName
          }
        }).then(res => {
          this.loading = false;
          this.companyList = res.data.records
        }).catch(() => {
        })
      },

      getPaymentTypeList() {
        // return this.$axios.get('/dict/page', {
        //   params: {
        //     order: 'ASC',
        //     orderField: 'createTime',
        //     current: 1,
        //     size: 20,
        //     listMode: true,
        //     queryItems: 'type,EQ,paymentType;parentId,GT,0'
        //   }
        // }).then(res => {
        //   this.loading = false;
        //   this.paymentTypes = res.records
        // }).catch(() => {
        // })
        this.getDicts("paymentType").then(response => {
          this.paymentTypes = response.data;
        });
      },

      changeOperation(val){
        if(val === 2){
          this.dataForm.paymentType = "6";
        }else{
          this.dataForm.paymentType = "1";
        }
      }

    }
  }
</script>
